package com.cj.image.service;

import com.cj.image.bean.ImageTableBean;
import com.cj.image.mapper.ImageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Service
@Slf4j
public class ImageService {
    @Autowired
    private ImageMapper imageMapper;

    public void getImageIo(String image_id, HttpServletResponse response){
        try {
            List<ImageTableBean> imageTableById = imageMapper.getImageTableById(image_id);

            // 检查是否查到了图片数据
            if (imageTableById == null || imageTableById.isEmpty()) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "未找到对应的图片");
                return;
            }

            ImageTableBean imageTableBean = imageTableById.get(0);
            Path imagePath = Paths.get(imageTableBean.getFile_path());

            // 检查文件是否存在
            if (!Files.exists(imagePath) || !Files.isReadable(imagePath)) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片文件不存在或不可读");
                return;
            }

            // 读取图片资源
            Resource resource = new UrlResource(imagePath.toUri());
            response.setContentType(Files.probeContentType(imagePath)); // 动态设置文件类型

            // 使用 `Files.copy` 更高效地将图片流写入响应
            try (InputStream inputStream = resource.getInputStream();
                 OutputStream outputStream = response.getOutputStream()) {
                byte[] buffer = new byte[8192]; // 使用缓冲区提高性能
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                outputStream.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
            try {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器错误，无法读取图片");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public PageInfo<ImageTableBean> getImageTableByFileMonthWithPagination(String fileMonth, int pageNum, int pageSize) {
        // 启动分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        List<ImageTableBean> imageTables = imageMapper.getImageTableByFileMonth(fileMonth);
        // 返回分页结果
        return new PageInfo<>(imageTables);
    }

    // 分页查询所有月份
    public PageInfo<String> getAllFileMonthWithPagination(int pageNum, int pageSize) {
        // 启动分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        List<String> months = imageMapper.getAllFileMonth();
        // 返回分页结果
        return new PageInfo<>(months);
    }
}
